class IUP_LABEL
-- Creates a label interface element, which displays a separator, a text or an 
-- image. Labels with images, texts or line separator can not change its 
-- behavior after mapped. But after map the image can be changed for another 
-- image, and the text for another text.

inherit
	IUP_WIDGET
		undefine
			execute_dragbegin,
			execute_dragdatasize,
			execute_dragdata,
			execute_dragend,
			execute_dropdata,
			execute_dropmotion
		redefine
			execute_map,
			execute_unmap,
			execute_destroy,
			execute_enterwindow,
			execute_leavewindow,
			execute_button,
			execute_motion,
			execute_dropfiles
		end
	IUP_WIDGET_ACTIVE
	IUP_WIDGET_BGCOLOR
	IUP_WIDGET_FGCOLOR
	IUP_WIDGET_TITLE
	IUP_WIDGET_FONT
	IUP_WIDGET_EXPAND
	IUP_WIDGET_SCREENPOSITION
	IUP_WIDGET_POSITION
	IUP_WIDGET_MAXMIN_SIZE
	IUP_WIDGET_TIP
	IUP_WIDGET_SIZE
	IUP_WIDGET_RASTERSIZE
	IUP_WIDGET_USERSIZE
	IUP_WIDGET_ZORDER
	IUP_WIDGET_VISIBLE
	IUP_WIDGET_CHILD
	IUP_WIDGET_NAME
	IUP_DRAG_AND_DROP
	IUP_WIDGET_CUSTOM_ATTRIBUTES
	IUP_WIDGET_PADDING

create {ANY}
   label_empty,
   label


feature {ANY}

	label_empty
		-- A new empty label.
		local
			a_label, p: POINTER
		do
			a_label := int_label (p)
			set_widget(a_label)	
		end
	
	label (text: STRING)
		-- A new label with the provided text
		local
			a_label: POINTER
		do
			a_label := int_label (text.to_external)
			set_widget(a_label)
		end

	-- Attributes

	set_alignment (horizontal, vertical: STRING)
		-- (non inheritable): horizontal and vertical alignment. Possible values: 
		-- "ALEFT", "ACENTER" and "ARIGHT",  combined to "ATOP", "ACENTER" and 
		-- "ABOTTOM". Default: "ALEFT:ACENTER". In Motif, vertical alignment is 
		-- restricted to "ACENTER".
		require
			is_valid_alignment(horizontal, vertical)
		local
			str: STRING
		do
			create str.copy(horizontal)
	      str.append_string(":")
 	      str.append_string(vertical)
	
			iup_open.set_attribute(Current, "ALIGNMENT", str)
		end

	set_as_drop_files_target (state: BOOLEAN)
		-- [Windows and GTK Only] (non inheritable): Enable or disable the drop 
		-- of files. Default: False, but if DROPFILES_CB is defined when the 
		-- element is mapped then it will be automatically enabled.
		do
			iup_open.set_attribute(Current, "DROPFILESTARGET", boolean_to_yesno(state))
		end

	set_ellipsis (state: BOOLEAN)
		-- [Windows and GTK only]: add an ellipsis: "..." to the text if there 
		-- is	not enough space to render the entire string. Can be "YES" or "NO". 
		-- Default: "False".
		do
			iup_open.set_attribute(Current, "ELLIPSIS", boolean_to_yesno(state))
		end

	set_image (name: STRING)
		-- (non inheritable): Image name. If set before map defines the behavior 
		-- of the button to contain an image. The natural size will be size of 
		-- the image in pixels. Use set_attribute_handle to associate an image to 
		-- a name. See also IUP_IMAGE.
		do
			iup_open.set_attribute(Current, "IMAGE", name)
		end

	set_image_inactive (name: STRING)
		-- (non inheritable) [GTK and Motif only]: Image name of the element when 
		-- inactive. If it is not defined then the IMAGE is used and the colors 
		-- will be replaced by a modified version of the background color 
		-- creating the disabled effect. GTK will also change the inactive image 
		-- to look like other inactive objects.
		do
			iup_open.set_attribute(Current, "IMINACTIVE", name)
		end

	set_markup (state: BOOLEAN)
		--  [GTK only]: allows the title string to contains pango markup 
		--  commands. Works only if a mnemonic is NOT defined in the title. Can 
		--  be "True" or "False". Default: "False". 
		do
			iup_open.set_attribute(Current, "MARKUP", boolean_to_yesno(state))
		end

	set_horizontal_separator
		-- (creation only) (non inheritable): Turns the label into a horizontal 
		-- line separator. When changed before mapping the EXPAND attribute is 
		-- set to "HORIZONTALFREE". (Since 3.11 changed to FREE based expand)
		do
			iup_open.set_attribute(Current, "SEPARATOR", "HORIZONTAL")
		end

	set_vertical_separator
		-- (creation only) (non inheritable): Turns the label into a vertical line
		-- separator. When changed before mapping the EXPAND attribute is set to 
		-- "VERTICALFREE" accordingly. (Since 3.11 changed to FREE based expand)
		do
			iup_open.set_attribute(Current, "SEPARATOR", "VERTICAL")
		end

	set_word_wrap (state: BOOLEAN)
		-- [Windows and GTK only]: enables or disable the wrapping of lines that 
		-- does not fits in the label. Can be "YES" or "NO". Default: "NO". Can 
		-- only set WORDWRAP=YES if ALIGNMENT=ALEFT.
		do
			iup_open.set_attribute(Current, "WORDWRAP", boolean_to_yesno(state))
		end

	-- Callbacks

	-- Common
	set_cb_map (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
		-- Called right after an element is mapped and its attributes updated.
		local
			operation: INTEGER
		do
			cb_map := act
			
			if cb_map /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
		end

	set_cb_unmap (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
		-- Called right before an element is unmapped.
		local
			operation: INTEGER
		do
			cb_unmap := act

			if cb_unmap /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
		end

	set_cb_destroy (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
		-- Called right before an element is destroyed.
		local
			operation: INTEGER
		do
			cb_destroy := act

			if cb_destroy /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
		end

	set_cb_enter_window (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
		-- Action generated when the mouse enters the native element. 
		local
			operation: INTEGER
		do
			cb_enterwindow := act

			if cb_enterwindow /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "ENTERWINDOW_CB", "NONEEDED", operation)
		end

	set_cb_leave_window (act: FUNCTION[TUPLE[IUP_LABEL], STRING])
		-- Action generated when the mouse leaves the native element.
		local
			operation: INTEGER
		do
			cb_leavewindow := act

			if cb_leavewindow /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "LEAVEWINDOW_CB", "NONEEDED", operation)
		end

	-- Extra
		set_cb_button (act: FUNCTION[TUPLE[IUP_LABEL, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING])
		-- Action generated when any mouse button is pressed and when it is 
		-- released. Both calls occur before the ACTION callback when button 1 is 
		-- being used.
		--
		-- IUP_BUTTON: identifies the element that activated the event.
		-- button: identifies the activated mouse button:
		--
		-- 1 - left mouse button (button 1);
		-- 2 - middle mouse button (button 2);
		-- 3 - right mouse button (button 3).
		--
		-- pressed: indicates the state of the button:
		--
		-- 0 - mouse button was released;
		-- 1 - mouse button was pressed.
		-- 
		-- x, y: position in the canvas where the event has occurred, in pixels.
		--
		-- status: status of the mouse buttons and some keyboard keys at the 
		-- moment the event is generated. The following IUP features must be used 
		-- for verification:
		--
		-- is_shift(status)
		-- is_control(status)
		-- is_button_1(status)
		-- is_button_2(status)
		-- is_button_3(status)
		-- is_button_4(status)
		-- is_button_5(status)
		-- is_double(status)
		-- is_alt(status)
		-- is_sys(status)
		--
		-- Returns: IUP_CLOSE will be processed. On some controls if IUP_IGNORE 
		-- is returned the action is ignored (this is system dependent).
		local
			operation: INTEGER
		do
			cb_button := act

			if cb_button /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "BUTTON_CB", "NONEEDED", operation)
		end

	set_cb_motion (act: FUNCTION[TUPLE[IUP_LABEL, INTEGER, INTEGER, STRING], STRING])
		-- Action generated when the mouse moves.
		-- ih: identifier of the element that activated the event.
		-- x, y: position in the canvas where the event has occurred, in pixels.
		-- status: status of mouse buttons and certain keyboard keys at the 
		-- moment the event was generated. The same macros used for BUTTON_CB can 
		-- be used for this status.
		local
			operation: INTEGER
		do
			cb_motion := act

			if cb_motion /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "MOTION_CB", "NONEEDED", operation)
		end

	set_cb_drop_files (act: FUNCTION[TUPLE[IUP_LABEL, STRING, INTEGER, INTEGER, INTEGER], STRING])
		local
			operation: INTEGER
		do
			cb_dropfiles := act

			if cb_dropfiles /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "DROPFILES_CB", "NONEEDED", operation)
		end

feature {IUP}

	execute_map: STRING
		do
			Result := cb_map.item([Current])
		end

	execute_unmap: STRING
		do
			Result := cb_unmap.item([Current])
		end

	execute_destroy: STRING
		do
			Result := cb_destroy.item([Current])
		end

	execute_enterwindow: STRING
		do
			Result := cb_enterwindow.item([Current])
		end

	execute_leavewindow: STRING
		do
			Result := cb_leavewindow.item([Current])
		end

	execute_button (btn, pressed, x, y: INTEGER; status: STRING): STRING
		do
			Result := cb_button.item([Current, btn, pressed, x, y, status])
		end

	execute_motion (x, y: INTEGER; status: STRING): STRING
		do
			Result := cb_motion.item([Current, x, y, status])
		end

	execute_dropfiles (filename: STRING; num: INTEGER; x: INTEGER; y: INTEGER): STRING
		do
			Result := cb_dropfiles.item([Current, filename, num, x, y])
		end
	
	
feature {}

	-- For callbacks
	
	cb_map: FUNCTION[TUPLE[IUP_LABEL], STRING]
	cb_unmap: FUNCTION[TUPLE[IUP_LABEL], STRING]
	cb_destroy: FUNCTION[TUPLE[IUP_LABEL], STRING]
	cb_enterwindow: FUNCTION[TUPLE[IUP_LABEL], STRING]
	cb_leavewindow: FUNCTION[TUPLE[IUP_LABEL], STRING]

	cb_button: FUNCTION[TUPLE[IUP_LABEL, INTEGER, INTEGER, INTEGER, INTEGER, STRING], STRING]
	cb_motion: FUNCTION[TUPLE[IUP_LABEL, INTEGER, INTEGER, STRING], STRING]
	cb_dropfiles: FUNCTION[TUPLE[IUP_LABEL, STRING, INTEGER, INTEGER, INTEGER], STRING]

	-- Internals
	
	int_label (text: POINTER): POINTER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupLabel"
         }"
      end

	-- Validations

	is_valid_alignment (horizontal, vertical: STRING): BOOLEAN
		local
			h, v: BOOLEAN
		do
			if horizontal.is_equal("ALEFT") or
				horizontal.is_equal("ACENTER") or
				horizontal.is_equal("ARIGHT") then
				h := True
			else
				h := False
			end

			if vertical.is_equal("ATOP") or
				vertical.is_equal("ACENTER") or
				vertical.is_equal("ABOTTOM") then
				v := True
			else
				v := False
			end

			if h and v then
				Result := True
			else
				Result := False
			end
		end
	
end -- class IUP_LABEL

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2018, 2019 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
